package com.nut.web;

import com.nut.common.Result;
import com.nut.pojo.Book;
import com.nut.pojo.Cart;
import com.nut.pojo.CartItem;
import com.nut.service.BookService;
import com.nut.service.impl.BookServiceImpl;
import com.nut.utils.JsonUtils;
import com.nut.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    /**
     * 加入购物车商品
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取图书编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //调用bookService方法得到图书对象
        Book book = bookService.queryBookById(id);
        //把图书对象转换为CartItem对象
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //调用Cart.addItem方法
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);
        resp.getWriter().write(JsonUtils.toJson(Result.ok().data("cart", cart).data("lastName", cartItem.getName())));
    }

    /**
     * 删除购物车商品
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            //删除购物车商品
            cart.deleteItem(id);
        }
    }

    /**
     * 清空购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            //清空购物车
            cart.clear();
        }
    }

    protected void listCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            resp.getWriter().write("error");
        } else {
            List<CartItem> list = cart.getList();
            String s = JsonUtils.toJson(Result.ok().data("cartList", list).data("totalSize", list.size()).data("totalPrice", cart.getTotalPrice()));
            resp.getWriter().write(s);
        }
    }
}
